﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

using XMK.Framework.Model.Ajax;
using XMK.Utils;
using XMK.Framework.Model;
using XMK.Framework.Config;

namespace XMK.Framework.Web
{
    /// <summary>
    /// Ajax请求的处理和相应类，在此类中实现动态路由业务逻辑方法并动态调用
    /// </summary>
    public class AjaxManager
    {
        /// <summary>
        /// Ajax初始化缓存对象，在程序启动时调用
        /// </summary>
        public static void Init()
        {
            if (File.Exists(Config.ConfigHelper.MappingFilePath))
            {
                AjaxMappingInfo ajaxMapping = AjaxMappingHelper.LoadAjaxMapping(Config.ConfigHelper.MappingFilePath);
                AjaxMappingHelper.InitAjaxMethod(ajaxMapping);
            }

            // 存在自动数据处理
            if (File.Exists(Config.ConfigHelper.MappingDataFilePath))
            {
                AjaxDataMappingInfo ajaxDataMapping = AjaxDataMappingHelper.LoadAjaxMapping(Config.ConfigHelper.MappingDataFilePath);
                AjaxDataMappingHelper.InitAjaxDataMethod(ajaxDataMapping);
            }
        }
        /// <summary>
        /// 执行服务端事件
        /// </summary>
        /// <param name="requestInfo">请求附加参数</param>
        /// <returns>执行结果对象</returns>
        internal static ResponseInfo Execute(RequestInfo requestInfo)
        {
            LogHelper.WriteLog(LogType.Debug, "AjaxManager反射处理开始：" + requestInfo.Type);
            AjaxMethod method = AjaxMappingHelper.GetAjaxMethod(requestInfo.Type);
            if (method == null)
            {
                LogHelper.WriteLog(LogType.Error, "找不到方法：" + requestInfo.Type);
                ResponseInfo responseInfo = new ResponseInfo("找不到方法" + requestInfo.Type);
                return responseInfo;
            }
            object result = null;
            object[] args = null;

            string parameterValues = requestInfo.Param;

            ParameterInfo[] paras = method.Method.GetParameters();

            if (paras != null && paras.Length > 0)
            {
                args = new object[paras.Length];

                for (int i = 0; i < paras.Length; i++)
                { 
                    if(paras[i].ParameterType == typeof(PageInfo))
                        args[i] = requestInfo.Page;
                    else if (paras[i].ParameterType == typeof(OrderInfo))
                        args[i] = requestInfo.ColOrder;
                    else if (paras[i].ParameterType.FullName.StartsWith("System."))
                        args[i] = ParseHelper.Parse(parameterValues, paras[i].ParameterType);
                    else
                    {
                        args[i] = JsonHelper.FromJson(parameterValues, paras[i].ParameterType);
                    }
                }
            }
            try
            {
                result = method.Method.Invoke(method.BizInstance, args);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogType.Error, "调用方法错误：" + requestInfo.Type + "--" + ex.ToString());
                return new ResponseInfo("出现异常，请联系管理员！");
            }
            LogHelper.WriteLog(LogType.Debug, "AjaxManager反射处理结束：" + requestInfo.Type);
            if (result is ResponseInfo)
                return result as ResponseInfo;
            else
                return new ResponseInfo() { Data = result, State = 0, Page = requestInfo.Page };
        }
    }
}